﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Services;
using Castle.Windsor;
using M2B.GestionEntretiens.Contracts;
using M2B.GestionEntretiens.Helpers.JSON;
using M2B.GestionEntretiens.Models;
using M2B.GestionEntretiens.POCOS;
using Newtonsoft.Json;

namespace M2B.GestionEntretiens.Controllers
{
    public class CandidatsController : Controller
    {


        #region Properties
        /// <summary>
        /// Candidats Model
        /// </summary>
        public CandidatsModel CandidatsModel
        {
            get
            {

                var candidatsModel = Session["CandidatsModel"] as CandidatsModel;
                if (candidatsModel == null)
                {
                    var container = Session["Container"] as IWindsorContainer;
                    if (container != null) candidatsModel = new CandidatsModel(container.Resolve<ICandidatsService>());
                    Session["CandidatsModel"] = candidatsModel;
                }
                return candidatsModel;

            }
            private set { Session["CandidatsModel"] = value; }
        }

        /// <summary>
        /// Pays des candidats
        /// </summary>
        public List<Referentiel> PaysCandidat
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "PAYS_CANDIDAT").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Langue des candidats
        /// </summary>
        public List<Referentiel> LangueCandidat
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "LANGUE_CANDIDAT").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Statut des candidats
        /// </summary>
        public List<Referentiel> StatutCandidat
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "STATUT_CANDIDAT").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        ///Formation d'un candidat
        /// </summary>
        public List<Referentiel> FormationCandidat
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "FORMATION_CANDIDAT").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Profil candidat
        /// </summary>
        public List<Referentiel> ProfilCandidat
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "TYPE_TEST").ToList();
                }
                return new List<Referentiel>();
            }
        }
        #endregion

        #region Actions
        //
        // GET: /Candidats/
        /// <summary>
        /// Vue candidats
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["PaysCandidat"] = PaysCandidat;
            ViewData["StatutCandidat"] = StatutCandidat;
            ViewData["FormationCandidat"] = FormationCandidat;
            ViewData["ProfilCandidat"] = ProfilCandidat;
            ViewData["LangueCandidat"] = LangueCandidat;
            return View();
        }
        /// <summary>
        /// Ajouter un test
        /// </summary>
        /// <returns></returns>
        public ActionResult AddCandidat()
        {
            ViewData["PaysCandidat"] = PaysCandidat;
            ViewData["StatutCandidat"] = StatutCandidat;
            ViewData["FormationCandidat"] = FormationCandidat;
            ViewData["ProfilCandidat"] = ProfilCandidat;
            ViewData["LangueCandidat"] = LangueCandidat;

            return PartialView(new Candidat() { Statut = "3",Pays = "1",Langue = "1",Formation = "3",Profil = "1",Experience = 1});
        }
        /// <summary>
        /// Ajouter un nouveau Candidat
        /// </summary>
        /// <param name="candidat">Candidat</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateCandidat(Candidat candidat)
        {
            try
            {
                CandidatsModel.CreateTest(candidat);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }
        /// <summary>
        /// Chargement de la liste des candidats
        /// </summary>
        /// <returns>Candidats</returns>
        public ActionResult LoadCandidats()
        {
            var strCandidats = JsonConvert.SerializeObject(CandidatsModel.Candidats,
                new EFNavigationPropertyConverter());
            return Json(strCandidats, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Rechercher des candidats 
        /// </summary>
        /// <returns>Candidats</returns>
        public ActionResult SearchCandidats(string textSearch)
        {
            var strCandidats = JsonConvert.SerializeObject(CandidatsModel.Candidats.Where(el => el.Prenom.ToUpper().Contains(textSearch.ToUpper()) || el.Nom.ToUpper().Contains(textSearch.ToUpper())),
                new EFNavigationPropertyConverter());
            return Json(strCandidats, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Supprimer un candidat
        /// </summary>
        /// <param name="candidatId">Candidat Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteCandidat(int candidatId)
        {
            try
            {
                CandidatsModel.DeleteCandidat(candidatId);
                return new EmptyResult();
            }
            catch
            {
                return View("Index");
            }
        }
        /// <summary>
        /// Modification d'un candidat
        /// </summary>
        /// <param name="candidatId">Id</param>
        /// <returns></returns>
        public ActionResult EditCandidat(int candidatId)
        {

            var candidat = CandidatsModel.GetCandidat(candidatId);
         
            ViewData["PaysCandidat"] = PaysCandidat;
            ViewData["StatutCandidat"] = StatutCandidat;
            ViewData["FormationCandidat"] = FormationCandidat;
            ViewData["ProfilCandidat"] = ProfilCandidat;
            ViewData["LangueCandidat"] = LangueCandidat;

            return PartialView(candidat);
        }
        //
        // POST: /Questions/Edit/5
        /// <summary>
        /// Submit de l'édition d'un candidat
        /// </summary>
        /// <param name="candidat">Candidat</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Candidat candidat)
        {
            try
            {
                CandidatsModel.Update(candidat);
                return new EmptyResult();
            }
            catch
            {
                return View("Index");
            }
        }
        #endregion
    }
}
